package com.example.controller;

import com.example.common.result.Result;
import com.example.config.MqttSendClient;
import com.alibaba.fastjson.JSONObject;
import com.example.entity.mqtt.Datasc;
import com.example.entity.mqtt.Mqunit;
import com.example.entity.mqtt.MqunitHis;
import com.example.service.DatascService;
import com.example.service.MqunitHisService;
import com.example.service.MqunitService;
import com.example.service.impl.SimpleMasterExample;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description : 测试类
 * @Author : Hdx
 * @Date : 2023/8/1 16:35
 */
@CrossOrigin // 跨域
@RestController
@RequestMapping("/api/mqtt")
@Api(tags = "mqtt硬件协议控制器")
public class MqttController {

    @Autowired
    private MqttSendClient mqttSendClient;
    @Resource
    private MqunitService mqunitService;
    @Resource
    private MqunitHisService mqunitHisService;
    @Resource
    private SimpleMasterExample simpleMasterExample;

    @PostMapping("/publishTopic")
    @ApiOperation("模拟客户端测试,给服务器发送消息")
    public String publishTopic(@RequestParam(defaultValue = "1") String topic, @RequestBody Datasc datasc) throws IOException {
        System.out.println("topic:" + topic);
        System.out.println("message:" + datasc);
        Object parse = JSONObject.toJSON(datasc);
        this.mqttSendClient.publish(false, topic, parse.toString());
        return "topic:" + topic + "\nmessage:" + parse.toString();
    }

    @GetMapping("XiaFa")
    @ApiOperation("下发命令")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", dataType = "Long", dataTypeClass = String.class),
            @ApiImplicitParam(name = "know", value = "区分计数和米数(0为计数,1为米数)", dataType = "Integer", dataTypeClass = String.class),
    })
    public Result XiaFa(Long  id,Integer know){
        try {
            Mqunit mqunit = this.mqunitService.queryByid(id);
            if (mqunit!=null){
                String string = this.simpleMasterExample.writeDemo2(mqunit.getIp(), mqunit.getPort(), mqunit.getCunqi());
                if (string.equals("0x000000")){
                    MqunitHis mqunitHis =new MqunitHis();
                    mqunitHis.setMid(mqunit.getId());
                    mqunitHis.setName(mqunit.getName());
                    mqunitHis.setCount(mqunit.getCount());
                    mqunitHis.setLength(mqunit.getLength());
                    mqunitHis.setStatus(mqunit.getStatus());
                    mqunitHis.setReset(mqunit.getReset());
                    SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String format = dateFormat.format(new Date());
                    mqunitHis.setTime(format);
                    if (know==1){
                        mqunit.setLength(0.00);
                    }else {
                        mqunit.setCount(0);
                    }
                    boolean b = this.mqunitService.updateById(mqunit);
                    Result insert = this.mqunitHisService.insert(mqunitHis);
                    if (insert.getCode().equals("200")&&b){
                        return Result.success("成功下发");
                    }
                    return Result.success("下发成功,插入历史表失败或修改数据失败");
                }
                return Result.error("下发失败");
            }
            return Result.error("该id不存在,下发失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("下发异常报错信息"+e);
        }
    }
    @PostMapping("/queryIPageList")
    @ResponseBody
    @ApiOperation("mqtt硬件设备分页查询")
    public Result<Map<String,Mqunit>> queryIPageList(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "1") Integer size){
        return this.mqunitService.queryIPageList(current,size);
    }

    @PostMapping("/queryPageListHis")
    @ResponseBody
    @ApiOperation("mqtt硬件设备清零历史分页查询")
    public Result<Map<String,MqunitHis>> queryPageListHis(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "1") Integer size){
        return this.mqunitHisService.queryPageListHis(current,size);
    }

    @GetMapping("/queryList")
    @ResponseBody
    @ApiOperation("mqtt硬件设备查询（没分页）")
    public  Result<List<Mqunit>> queryList(){
        return this.mqunitService.queryList();
    }

    @GetMapping("/queryListHis")
    @ResponseBody
    @ApiOperation("mqtt硬件设备清零历史查询（没分页）")
    public  Result<List<MqunitHis>> queryListHis(){
        return this.mqunitHisService.queryListHis();
    }

    @GetMapping("/queryById/{id}")
    @ResponseBody
    @ApiOperation("通过主键id进行详细查询硬件数据")
    public Result queryById(@PathVariable Long id){
        return this.mqunitService.queryById(id);
    }
}
